package panel.viewSpots.navigationNew
{
	import assists.HtmlText;
	import assists.TextFieldUtils;
	import assists.resource.ResourceLoader;
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.LocationChangeEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.media.StageWebView;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.TextField;
	import flash.ui.Keyboard;
	
	import main.ClientConstants;
	
	import struct.Hotel;
	import struct.Restaurant;
	import struct.Scenic;
	import struct.ScenicShop;
	
	public class ViewSpotDetailItem extends Sprite
	{
		private var _stage:Stage;
		private var _loader:ResourceLoader;
		
		private var _arrow:Bitmap;
		private var _line:Bitmap;
		private var _icon:Bitmap;
		private var _showTxt:TextField = new TextField();
		
		private static const TYPE_TIME:String = "time";
		private static const TYPE_ADDRESS:String = "address";
		private static const TYPE_TICKET:String = "ticket";
		private static const TYPE_TRAFFIC:String = "traffic";
		private static const TYPE_WEB:String = "website";
		private static const TYPE_PHONE:String = "phone";
		private static const TYPE_DESC:String = "desc";
		private static const TYPE_COMMENT:String = "commentList";
		private static const TYPE_COSTNUM:String = "costNum";
		private static const TYPE_MAP:String = "map";
		
		private static var DIS_X:int;
		private static var DIS_Y:int;
		private var _data:Object;
		private var _webView:StageWebView;
		private var _webList:int;
		private var _openTextPanel:Function;
		private var _openCOmmitPanel:Function;
		private var _priStr:String = "";
		private var _openMap:Function;
		
		private var onShowLocation:Function;
		
		public var parentStruct:*;
		
		public function ViewSpotDetailItem(stage:Stage,loader:ResourceLoader)
		{
			_stage = stage;
			_loader = loader;
			DIS_X = _stage.stageWidth * 0.02;
			DIS_Y = _stage.stageHeight * 0.02;
			
			init();
		}
		
		private function init():void
		{
			_arrow = new _loader.uiManager.detailPng();
			_arrow.width = _arrow.height = _stage.stageWidth * 0.05;
			_line = new _loader.uiManager.cutLine();
			_line.width = _stage.stageWidth * 0.9;
			_line.x = _stage.stageWidth * 0.05;
			
			_showTxt.mouseEnabled = false;
			_showTxt.width = _stage.stageWidth * 0.8;
			_showTxt.multiline = true;
			_showTxt.wordWrap = true;
			_showTxt.autoSize = "left";
			
		}
		/**
		 * @param data {desc:""这里是介绍,type:1}
		 */		
		public function show(data:Object):void
		{
			_data = data;
			chooseIcon(data);
			
			if(data.desc is String && data.desc.length > 80)
			{
				TextFieldUtils.setHtmlText(_showTxt,HtmlText.formatSizeColor(data.desc.slice(0,80) + "...",0xffffff));
			}else
			{
				TextFieldUtils.setHtmlText(_showTxt,HtmlText.formatSizeColor(_priStr +　data.desc,0xffffff));
			}
			layout();
			this.addEventListener(MouseEvent.MOUSE_UP,clickHandler);
		}
		
		public function updateText(data:Object):void
		{
			if(data.desc.length == 0) return;
			data.desc = "共有" + ((data.desc.length - 1) * 8 + data.desc[data.desc.length - 1].length)+ "人评价";;
			TextFieldUtils.setHtmlText(_showTxt,HtmlText.formatSizeColor(_priStr +　data.desc,0xffffff));
		}
		
		protected function clickHandler(event:MouseEvent):void
		{
			if(_data.type == TYPE_PHONE)
			{
				navigateToURL(new URLRequest("tel:"+_data.desc));
			}else if(_data.type == TYPE_WEB)
			{
				if(!_webView) _webView = new StageWebView();
				_webView.stage = _stage;
				_webView.viewPort = new Rectangle(0,0,_stage.stageWidth,_stage.stageHeight);
				_webView.loadURL(_data.desc);
				_stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
			}else if(_data.type == TYPE_DESC || _data.type == TYPE_TRAFFIC)
			{
				_openTextPanel(_data.desc);
			}else if(_data.type == TYPE_COMMENT)
			{
				_openCOmmitPanel();
			}else if(_data.type == TYPE_ADDRESS)
			{
				//				var url:String = "http://ditu.google.cn/?ll=" + _data.value[0] + "," + _data.value[1] + "&z=15";
				//				navigateToURL(new URLRequest(url));
				
				if(onShowLocation is Function){
					var obj:*;
					if(parentStruct.type == ClientConstants.VISIT_TYPE_SCENIC){
						obj = new Scenic;
						obj = Scenic(parentStruct);
					}
					if(parentStruct.type == ClientConstants.VISIT_TYPE_EATING){
						obj = new Restaurant;
						obj = Restaurant(parentStruct);
					}
					if(parentStruct.type == ClientConstants.VISIT_TYPE_FUN){
						obj = new ScenicShop;
						obj = ScenicShop(parentStruct);
					}
					if(parentStruct.type == ClientConstants.VISIT_TYPE_HOTEL){
						obj = new Hotel;
						obj = Hotel(parentStruct);
					}	
					
					onShowLocation(parentStruct);
				}
				
			}else if(_data.type == TYPE_MAP)
			{
				_openMap({id:_data.value});
			}else if(_data.type == TYPE_TICKET)
			{
				_openTextPanel(_data.desc + "\n" + _data.value);
			}
		}
		
		protected function htmlPageLocationChangeHandler(event:LocationChangeEvent):void
		{
			trace("111");
		}
		
		protected function keyDownHandler(event:KeyboardEvent):void
		{
			if(event.keyCode == Keyboard.BACK)
			{
				event.stopPropagation();
				if(_webView.isHistoryBackEnabled)
				{
					_webView.historyBack();
					return;
				}
				_webView.stage = null;
				_webView.dispose();
				_stage.removeEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
			}
		}
		
		private function chooseIcon(data:Object):void
		{
			switch(data.type)
			{
				case TYPE_ADDRESS:
					_icon = new _loader.uiManager.dingweiPng();
					break;
				case TYPE_PHONE:
					_icon = new _loader.uiManager.phonePng();
					break;
				case TYPE_TICKET:
					_icon = new _loader.uiManager.ticketPng();
					break;
				case TYPE_TIME:
					_icon = new _loader.uiManager.timePng();
					_arrow.visible = false;
					break;
				case TYPE_TRAFFIC:
					_icon = new _loader.uiManager.trafficPng();
					break;
				case TYPE_WEB:
					_icon = new _loader.uiManager.webPng();
					break;
				case TYPE_DESC:
					_icon = new Bitmap();
					break;
				case TYPE_COSTNUM:
					_priStr = "人均消费:";
					_icon = new _loader.uiManager.ticketPng();
					_arrow.visible = false;
					break;
				case TYPE_MAP:
					_icon = _loader.uiManager.generateBmp(_loader.uiManager.mapPng);
					data.value = data.desc;
					data.desc = "查看景区地图";
					break;
				case TYPE_COMMENT:
					_icon = new _loader.uiManager.commitPng();
					if(data.desc.length == 0)
					{
						data.desc = "暂时没有评论哦";
					}else
					{
						data.desc = "共有" + ((data.desc.length - 1) * 8 + data.desc[data.desc.length - 1].length)+ "人评价";;
					}
					break;
			}
			_icon.width = _icon.height = _stage.stageWidth * 0.05;
		}
		
		private function layout():void
		{
			//			this.graphics.beginFill(0);
			//			this.graphics.drawRect(0,0,_stage.stageWidth,2);
			//			this.graphics.endFill();
			
			_showTxt.x = _icon.x + _icon.width + DIS_X;
			_showTxt.y = DIS_Y;
			
			_arrow.x = _stage.stage.width * 0.95 - _arrow.width;
			_arrow.y = _showTxt.y + (_showTxt.height - _arrow.height) * 0.5;
			
			_line.y = _showTxt.y + _showTxt.height + DIS_Y;
			
			_icon.y = _showTxt.y + 2;
			
			this.addChild(_showTxt);
			this.addChild(_arrow);
			this.addChild(_line);
			this.addChild(_icon);
		}
		
		public function set openTextPanel(value:Function):void
		{
			_openTextPanel = value;
		}
		
		public function set openCommitPanel(value:Function):void
		{
			_openCOmmitPanel = value;
		}
		
		public function set openMap(value:Function):void
		{
			_openMap = value;
		}
		
		public function set showLocation(func:Function):void
		{
			onShowLocation = func;
		}
		
		public function clear():void
		{
			this.removeEventListener(MouseEvent.CLICK,clickHandler);
		}
		
	}
}